package com.zbkj.crmeb.store;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public abstract class FeatureBaseDO {
    @TableField(exist = false)
    private Map<String, String> featureMap;

    public FeatureBaseDO() {
    }

    public Map<String, String> getFeatureMap() {
        if (this.featureMap != null && this.featureMap.size() > 0) {
            return this.featureMap;
        } else {
            Map<String, String> featureMap = new HashMap();
            if (StringUtils.isBlank(this.getFeatureString())) {
                return (Map)featureMap;
            } else {
                try {
                    featureMap = (Map) JSONObject.parseObject(this.getFeatureString(), Map.class);
                } catch (Exception var3) {
                }

                return (Map)featureMap;
            }
        }
    }

    public String getFeature(String name) {
        Map<String, String> featureMap = this.getFeatureMap();
        String value = (String)featureMap.get(name);
        return value == null ? "" : value;
    }

    public void addFeature(String name, String value) {
        if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
            this.featureMap = this.getFeatureMap();
            this.featureMap.put(name, value);
            this.resetFeature();
        }

    }

    public boolean removeFeature(String name) {
        if (StringUtils.isBlank(name)) {
            return true;
        } else {
            this.featureMap = this.getFeatureMap();
            if (this.featureMap.containsKey(name)) {
                this.featureMap.remove(name);
                this.resetFeature();
                return true;
            } else {
                return false;
            }
        }
    }

    public void addFeatureMap(Map<String, String> map) {
        if (map != null && !map.isEmpty()) {
            this.featureMap = this.getFeatureMap();
            this.featureMap.putAll(map);
            this.resetFeature();
        }

    }

    protected abstract void resetFeature();

    protected abstract String getFeatureString();
}